<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- saved from url=(0028)http://jdbi.org/archive.html -->
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></meta>


<title>abacus-common</title>

<script type="text/javascript" src="./css/ga.js"></script>
<script type="text/javascript">
	if (top.location != self.location)
		top.location.replace(self.location);
</script>

<link href="./css/css" rel="stylesheet" type="text/css"></link>
<link href="./css/css(1)" rel="stylesheet" type="text/css"></link>
<link rel="stylesheet" href="./css/syntax.css" type="text/css"></link>
<link rel="stylesheet" href="./css/screen.css" type="text/css"></link>

<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-65247258-1', 'auto');
  ga('send', 'pageview');
</script>

</head>
<body>
	<div id="site">
		<h1>
			<a href="./index.html">abacus-common</a> <span id="navigation"> <a href="./docs.html">Docs</a> | <a href="./download.html">Download</a>
				| <a href="./api-docs/index.html">Javadoc</a> | <a href="./faq.html">FAQ</a> | <a href="./community.html">Community</a>
			</span>
		</h1>
	</div>

	<div id="content">

		<div id="page">

			<h1>JdbcUtil/CSVUtil</h1>
			<p>
				<a href="./JdbcUtil_view.html">JdbcUtil</a>/<a href="./CSVUtil_view.html">CSVUtil</a> provides the methods to create data source (manager), prepare/execute statement, import/export data with (CSV/JSON) format...:
			</p>
			
			
			<li>create data source (manager) with xml configuration. Here is the schema: <a href="./DataSource.xsd">DataSource.xsd</a></li>
<!-- HTML generated using hilite.me --><div style="background: #f8f8f8; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><pre style="margin: 0; line-height: 125%"><span style="color: #BC7A00">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span>

<span style="color: #008000; font-weight: bold">&lt;dataSourceManager</span> <span style="color: #7D9029">liveEnv=</span><span style="color: #BA2121">&quot;dev&quot;</span><span style="color: #008000; font-weight: bold">&gt;</span>
    <span style="color: #008000; font-weight: bold">&lt;dataSource</span> <span style="color: #7D9029">name=</span><span style="color: #BA2121">&quot;codes&quot;</span> <span style="color: #7D9029">env=</span><span style="color: #BA2121">&quot;dev&quot;</span><span style="color: #008000; font-weight: bold">&gt;</span>
        <span style="color: #008000; font-weight: bold">&lt;sqlLog&gt;</span>true<span style="color: #008000; font-weight: bold">&lt;/sqlLog&gt;</span>
        <span style="color: #008000; font-weight: bold">&lt;connection&gt;</span>
            <span style="color: #008000; font-weight: bold">&lt;driver&gt;</span>org.h2.Driver<span style="color: #008000; font-weight: bold">&lt;/driver&gt;</span>
            <span style="color: #008000; font-weight: bold">&lt;url&gt;</span>jdbc:h2:tcp://localhost/~/codes<span style="color: #008000; font-weight: bold">&lt;/url&gt;</span>
            <span style="color: #008000; font-weight: bold">&lt;user&gt;</span>SA<span style="color: #008000; font-weight: bold">&lt;/user&gt;</span>
            <span style="color: #008000; font-weight: bold">&lt;password&gt;&lt;/password&gt;</span>
            <span style="color: #008000; font-weight: bold">&lt;liveTime&gt;</span>86400000<span style="color: #008000; font-weight: bold">&lt;/liveTime&gt;</span>
            <span style="color: #008000; font-weight: bold">&lt;maxIdleTime&gt;</span>1800000<span style="color: #008000; font-weight: bold">&lt;/maxIdleTime&gt;</span>
            <span style="color: #008000; font-weight: bold">&lt;maxActive&gt;</span>100<span style="color: #008000; font-weight: bold">&lt;/maxActive&gt;</span>
        <span style="color: #008000; font-weight: bold">&lt;/connection&gt;</span>
    <span style="color: #008000; font-weight: bold">&lt;/dataSource&gt;</span>
<span style="color: #008000; font-weight: bold">&lt;/dataSourceManager&gt;</span>
</pre></div>


<!-- HTML generated using hilite.me --><div style="background: #f8f8f8; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><pre style="margin: 0; line-height: 125%"><span style="color: #008000; font-weight: bold">static</span> <span style="color: #008000; font-weight: bold">final</span> String dataSourcePath <span style="color: #666666">=</span> <span style="color: #BA2121">&quot;./resources/config/abacus/h2_dataSource.xml&quot;</span><span style="color: #666666">;</span>

<span style="color: #008000; font-weight: bold">static</span> <span style="color: #008000; font-weight: bold">final</span> DataSourceManager dsm <span style="color: #666666">=</span> JdbcUtil<span style="color: #666666">.</span><span style="color: #7D9029">createDataSourceManager</span><span style="color: #666666">(</span>dataSourcePath<span style="color: #666666">);</span>
<span style="color: #008000; font-weight: bold">static</span> <span style="color: #008000; font-weight: bold">final</span> DataSource ds <span style="color: #666666">=</span> dsm<span style="color: #666666">.</span><span style="color: #7D9029">getPrimaryDataSource</span><span style="color: #666666">();</span>
<span style="color: #408080; font-style: italic">// Or: static final DataSource ds = JdbcUtil.createDataSource(dataSourcePath);</span>

<span style="color: #008000; font-weight: bold">static</span> <span style="color: #008000; font-weight: bold">final</span> SQLExecutor sqlExecutor <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">new</span> SQLExecutor<span style="color: #666666">(</span>ds<span style="color: #666666">);</span>
</pre></div>



			<p></p>
			<li>export/import to/from CSV</li>
<!-- HTML generated using hilite.me --><div style="background: #f8f8f8; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><pre style="margin: 0; line-height: 125%">sqlExecutor<span style="color: #666666">.</span><span style="color: #7D9029">batchInsert</span><span style="color: #666666">(</span>INSERT_ACCOUNT<span style="color: #666666">,</span> accounts<span style="color: #666666">);</span>

Connection conn <span style="color: #666666">=</span> sqlExecutor<span style="color: #666666">.</span><span style="color: #7D9029">getConnection</span><span style="color: #666666">();</span>
File file <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">new</span> File<span style="color: #666666">(</span><span style="color: #BA2121">&quot;./unittest/test.csv&quot;</span><span style="color: #666666">);</span>
String sql <span style="color: #666666">=</span> <span style="color: #BA2121">&quot;SELECT first_name, last_name, gui, last_update_time, create_time FROM account&quot;</span><span style="color: #666666">;</span>
CSVUtil<span style="color: #666666">.</span><span style="color: #7D9029">exportCSV</span><span style="color: #666666">(</span>file<span style="color: #666666">,</span> conn<span style="color: #666666">,</span> sql<span style="color: #666666">);</span>

sqlExecutor<span style="color: #666666">.</span><span style="color: #7D9029">batchUpdate</span><span style="color: #666666">(</span>DELETE_ACCOUNT_BY_ID<span style="color: #666666">,</span> accounts<span style="color: #666666">);</span>

List<span style="color: #666666">&lt;</span>Type<span style="color: #666666">&lt;</span>Object<span style="color: #666666">&gt;&gt;</span> types <span style="color: #666666">=</span> N<span style="color: #666666">.</span><span style="color: #7D9029">getType</span><span style="color: #666666">(</span>String<span style="color: #666666">.</span><span style="color: #7D9029">class</span><span style="color: #666666">,</span> String<span style="color: #666666">.</span><span style="color: #7D9029">class</span><span style="color: #666666">,</span> String<span style="color: #666666">.</span><span style="color: #7D9029">class</span><span style="color: #666666">,</span> Timestamp<span style="color: #666666">.</span><span style="color: #7D9029">class</span><span style="color: #666666">,</span> Timestamp<span style="color: #666666">.</span><span style="color: #7D9029">class</span><span style="color: #666666">);</span>
sql <span style="color: #666666">=</span> <span style="color: #BA2121">&quot;INSERT INTO account (first_name, last_name, gui, last_update_time, create_time) VALUES ( ?,  ?,  ?, ?, ?)&quot;</span><span style="color: #666666">;</span>
CSVUtil<span style="color: #666666">.</span><span style="color: #7D9029">importCSV</span><span style="color: #666666">(</span>file<span style="color: #666666">,</span> conn<span style="color: #666666">,</span> sql<span style="color: #666666">,</span> types<span style="color: #666666">);</span>
</pre></div>

			
		<p></p>
		<p>It's easy/simple to handle huge data with <a href="./api-docs/com/landawn/abacus/util/SQLExecutor.html">SQLExecutor</a> and <a href="./api-docs/com/landawn/abacus/util/JdbcUtil.html">JdbcUtil</a>/<a href="./api-docs/com/landawn/abacus/util/CSVUtil.html">CSVUtil</a> (through CSV file or <a href="./api-docs/com/landawn/abacus/DataSet.html">DataSet</a>)</p>

		</div>

	</div>
</body>

</html>